using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace AntEater.ComponentModel.BuildElements
{
    /// <summary>
    /// Provides an representation of an Task Parameter.
    /// </summary>
    [Serializable()]
    public class TaskParameter:INotifyPropertyChanged,ICloneable
    {
        /// <summary>
        /// Occurs when a property has changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:TaskParameter"/> class.
        /// </summary>
        public TaskParameter()
            :this(null,null) {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:TaskParameter"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public TaskParameter(string name)
            :this(name,null){
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:TaskParameter"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="value">The value.</param>
        public TaskParameter(string name, string value) {
            this._name = name;
            this._value = value;
        }

        private string _name;

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name {
            get { return _name; }
            set {
                if (_name != value) {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        private string _value;

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public string Value {
            get { return _value; }
            set {
                if (_value != value) {
                    _value = value;
                    OnPropertyChanged("Value");
                }
            }
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone() {

            string newName = null;
            string newValue = null;

            if (this._name != null) {
                newName = this._name.Clone() as string;
            }
            if (this._value != null) {
                newValue = this._value.Clone() as string;
            }

            TaskParameter newParameter
                = new TaskParameter(newName, newValue);

            return newParameter;
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString() {
            return this.Name + " = " + this.Value;
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName) {
            if (this.PropertyChanged != null) {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
